﻿using FI.Core.ApplicationContexts;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using AutoMapper;
using FI.EntityFramework;
using FI.AspNet.Identity.EntityFramework;
using System.Data.Entity;

namespace FI.Security
{
    public class SiteMapStore : Repository<Sitemap, SecurityContext>
    {
        public SiteMapStore()
            : base(new SecurityContext())
        {
            Mapper.Initialize(cfg =>
                {
                
                    cfg.CreateMap<Sitemap, SitemapModel>().ReverseMap();

                });
        }

        public List<SitemapModel> GetSitemaps()
        {
            List<SitemapModel> list = new List<SitemapModel>();

            var parents = this.DbContext.Sitemaps.Where(n => n.SitemapParent == n.Id && n.Status == 1).OrderBy(n => n.OrderBy).ToList();

            foreach (var parent in parents)
            {
                var children = this.DbContext.Sitemaps.Where(n => n.SitemapParent == parent.Id && n.SitemapParent != n.Id && n.Status == 1).OrderBy(n => n.OrderBy).ProjectToList<SitemapModel>();

                list.Add(Mapper.Map<SitemapModel>(parent));

                list.AddRange(children);

            }
            return list;

        }

        public IList<SitemapModel> GetUserSiteMaps(string username)
        {
            var currentuser = DbContext.Users.Include("CM_Role")
                .Include("CM_Role.CM_Sitemap").Where(n => n.UserName == username  ).SingleOrDefault();

            if (currentuser == null) throw new Exception("cannot find current user info.");

            List<Sitemap> sitemaps = new List<Sitemap>();

            foreach (var item in currentuser.Roles)
            {
                sitemaps.AddRange(item.Sitemaps);
            }
            return AutoMapper.Mapper.Map<List<SitemapModel>>(sitemaps);

        }

        public IList<User> GetAllUsersWithSiteMap()
        {
            var users = DbContext.Users.Include("CM_Role").Include("CM_Role.CM_Sitemap").ToList();

            return users;

        }

        public SitemapModel GetSitemap(string id)
        {
            return this.DbContext.Sitemaps.Where(n => n.Id == id).ProjectToSingle<SitemapModel>();
        }
 
        

        public void AddSitemap(SitemapModel sitemapModel)
        {
            var entity = Mapper.Map<Sitemap>(sitemapModel);

            var entry = this.DbContext.Entry<Sitemap>(entity);

            entry.State = EntityState.Added;

            DbContext.SaveChanges();
        }
        public void UpdateSitemap(SitemapModel sitemapModel)
        {
            var entity = Mapper.Map<Sitemap>(sitemapModel);

            this.DbContext.Set<Sitemap>().Attach(entity);

            var entry = this.DbContext.Entry<Sitemap>(entity);

            entry.State = EntityState.Modified;

            DbContext.SaveChanges();
        }
        public void DeleteSitemap(string sitemapId)
        {
            var entity = this.DbContext.Sitemaps.SingleOrDefault(n => n.Id == sitemapId);

            var entry = this.DbContext.Entry<Sitemap>(entity);

            entry.State = EntityState.Deleted;

            DbContext.SaveChanges();
        }
    }
}
